<template>
  <div class="bill">
    <pass @canShow="show"></pass>
    <div v-show="ishow" class="table" ref='tableRef'>
      <div >
        <el-row class="row">
          <el-col :span="6">
            <span>月 份</span>
            <el-date-picker v-model="month" type="month" placeholder="选择月">
            </el-date-picker>
          </el-col>
          <el-col :span="6"><span>支付宝</span><input type="text" placeholder="支付宝消费" v-model="zhi"/></el-col>
          <el-col :span="6"><span>微 信</span><input type="text"  placeholder="微信消费" v-model='wei'/></el-col>
          <el-col :span="6"><span>其 他</span><input type="text"  placeholder="其他消费" v-model='oth'/></el-col>
        </el-row>
        <el-row class="row brow">
          <el-col :span="10">
            <span class="bei">备 注</span>
            <!-- <textarea v-model="text" placeholder="当月消费备注"></textarea> -->
            <el-input
                type="textarea"
                :rows="2"
                placeholder="当月消费备注"
                v-model="text">
            </el-input>
          </el-col>
          <el-col :span='2'>
          </el-col>
          <el-col :span="12" >
            <el-button type='primary' size='mini' @click='addBill'>增加</el-button>
            <el-button type='primary' size='mini' plain @click='resetBill'>重置</el-button>
          <!-- </el-col>
          <el-col :span='7' > -->
            <el-button type="primary" icon="el-icon-s-promotion" @click='goto'>账单大屏</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table :data="tableData.slice(currentPage-1,currentPage+pageSize-1)" style="width: 100%" @cell-mouse-enter="tenter" @cell-mouse-leave="tleave">
        <el-table-column label="消费账单" align="center">
          <el-table-column fixed prop="month" label="月份" :width="width">
          </el-table-column>
          <el-table-column prop="zhi" label="支付宝" :width="width">
          </el-table-column>
          <el-table-column prop="wei" label="微信" :width="width">
          </el-table-column>
          <el-table-column prop="oth" label="其他" :width="width">
          </el-table-column>
          <el-table-column prop="total" label="合计" :width="width">
          </el-table-column>
          <el-table-column prop="date" label="记录日期" :width="width">
          </el-table-column>
          <el-table-column prop="text" label="备注" :width="width*2">
          </el-table-column>
        </el-table-column>
      </el-table>
      <div class="yeFoot">
        <div class="down" @click='downLoad'>
          <i class="el-icon-download">数据下载</i>
        </div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length">
        </el-pagination>
      </div>
      
    </div>
  </div>
</template>
  
<script>
import { computed, getCurrentInstance, onMounted, onBeforeUnmount, reactive, toRefs,watch, watchEffect } from "vue";
import {useRouter} from 'vue-router'
import pass from "../pass.vue";
import passhow from "../../utils/canshow";
export default {
  components: {
    pass,
  },
  setup() {
    let {ctx}=getCurrentInstance()
    let router=useRouter()
    onMounted(()=>{
      
      let date=new Date()
      let year=date.getFullYear()
      let mon=date.getMonth()+1
      mon=mon>10?mon:'0'+mon
      state.month=year+'-'+mon
       getTableData()
       
    })
    window.addEventListener('resize',()=>{getWidth()})
    let state = reactive({
      month:'',
      tableData: [ ],
      width:0,
      currentPage:1,
      pageSize:5

    });

    let getTableData=async ()=>{
       let res=await ctx.$axios.get('/bill')
       if(res.status==200){
         state.tableData=res.data
         getWidth()
       }
    }
    let getWidth=()=>{
       let ww=ctx.$refs.tableRef?.offsetWidth-20
       state.width=ww>600?ww/8:600/8
    }
    
    let goto=()=>{
       router.push('/billScreen')
    }

    //分页
    let handleSizeChange=(val)=>{
       state.pageSize=val
    }
    let handleCurrentChange=(val)=>{
      state.currentPage=val
    }
    let newDate=reactive({
      zhi:'',
      wei:'',
      oth:'',
      text:''
    })
    let addBill=async ()=>{
       if(!state.month||!newDate.zhi||!newDate.wei||!newDate.oth){
         ctx.$message.warning('请完整填写信息')
         return
       }
       let newBill={}
       if(typeof state.month=='string'){
         newBill.month=state.month
       }else{
         let year=state.month.getFullYear()
         let month=state.month.getMonth()+1
         month=month>10?month:'0'+month
         newBill.month=year+'-'+month
       }
       let date=new Date()
       let mm=date.getMonth()+1
       let dd=date.getDate()
       let todate=mm+'-'+dd
       let tBill=Number(newDate.zhi) +Number(newDate.wei) +Number(newDate.oth) 
       newBill.zhi=Number(newDate.zhi)
       newBill.wei=Number(newDate.wei)
       newBill.oth=Number(newDate.oth) 
       newBill.total=tBill
       newBill.date=todate
       newBill.text=newDate.text
       
       let res=await ctx.$axios.post('/addbill',newBill)
       if(res.status=200){
         ctx.$message.success('提交成功')
         let nres=await ctx.$axios.get('/bill')
         if(nres.status=200){
           state.tableData=nres.data
         }
       }else{
         ctx.$message.error('信息提交失败')
       }


    }
    let resetBill=()=>{
       newDate.zhi=''
       newDate.wei=''
       newDate.oth=''
       newDate.text=''
    }
    //数据下载
    let downLoad=()=>{
      console.log(55555)
      let table=`
        <table border='1px solid gray' width='600'>
           <tr>
             <th>月份</th>
             <th>支付宝</th>
             <th>微信</th>
             <th>其他</th>
             <th>合计</th>
             <th>登记日期</th>
             <th>备注</th>
           </tr>
      `
      state.tableData.forEach(item=>{
        table+=`
           <tr>
              <td>${item.month}</td>
              <td>${item.zhi}</td>
              <td>${item.wei}</td>
              <td>${item.oth}</td>
              <td>${item.total}</td>
              <td>${item.date}</td>
              <td>${item.text}</td>
           </tr>
        `
      })
      table+=`</table>`

      let downName='消费统计表格'
      let blob=new Blob([table],{type:'application/vnd.ms-excel'})

      blob=new Blob([String.fromCharCode(0xfeff),blob],{type:blob.type})
      let link=document.createElement('a')
      link.download=downName+'.xls'
      link.href=URL.createObjectURL(blob)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }

    onBeforeUnmount(()=>{
      window.removeEventListener("resize",{});
    })
    let tenter=function(row, column){
      if(column.label=="支付宝"){
           console.log(row,"8888", column)
      }
    }
    let tleave=function(r,column){
      if(column.label=="支付宝"){
           console.log("9999-----------", column)
      }
    }
    return {
      ...passhow(),
      ...toRefs(state),
      goto,
      handleSizeChange,handleCurrentChange,
      ...toRefs(newDate),addBill,resetBill,
      downLoad,tenter,tleave

    };
  },
};
</script>

<style lang='less' scoped>
.bill{
  padding: 10px;
  height: 460px;
  position: relative;
  .row{
    min-width: 600px;
    width: 100%;
    height: 40px;
    line-height: 40px;
    
    span{
      display: inline-block;
      width:50px;
      line-height: 40px;
      margin-right: 5px;
    }
    input{
      width: 70%;
      height: 26px;
    }
    .bei{
      float: left;
    }

  }
}
/deep/ .el-input--prefix .el-input__inner {
  width: 90%;
  height: 26px;
}
.el-col{
  overflow: hidden;
  white-space: nowrap;
}
.brow{
  margin-bottom:50px;
}
.el-button{
  margin-left:50px;
  padding:0 10px;
  height:26px;
  width: 100px;
  min-width: 80px;
}
.table{
    width:100%;
  }
  .yeFoot{
    position: relative;
    box-sizing: border-box;
    margin-top: 40px;
    width: 100%;
    padding-top: 5px;
    border-top: 1px solid #556598;
  }
  .down{
    position: absolute;
    right: 20px;
  }
  .down:hover{
    color:blue
  }
  /deep/.el-textarea__inner{
    width: 70%;
  }
</style>